import { Injectable, Injector } from '@angular/core';
import { Router } from '@angular/router';

import { NzModalService, NzMessageService } from 'ng-zorro-antd';

import { TranslateService } from '@ngx-translate/core';
import { SystemUserService } from '../system/system-user.service';
import { TokenService } from '../auth/token/token.service';

@Injectable()
export class ReturnMessage {

  constructor(
    private injector: Injector,
  ) { }

  private get nzMessage(): NzMessageService {
    return this.injector.get(NzMessageService);
  }

  private get nzModal(): NzModalService {
    return this.injector.get(NzModalService);
  }

  private get translate(): TranslateService {
    return this.injector.get(TranslateService);
  }

  private get router(): Router {
    return this.injector.get(Router);
  }

  private get systemUserService(): SystemUserService {
    return this.injector.get(SystemUserService);
  }

  private get tokenService(): TokenService {
    return this.injector.get(TokenService);
  }

  private showLoginTip = false;

  /**
   * 业务错误提示
   * @param {string} returnCode 返回码
   */
  verification(returnCode: string, tips: boolean = true): void {
    switch (returnCode) {
      case '000000':
        break;
      case '010001':
        // 未登录
        this.getLoginTip();
        break;
      default:
        this.getError(returnCode);
        break;
    }
  }

  /**
   * 弹出登录超时
   */
  getLoginTip(): void {
    if (!this.showLoginTip) {
      this.showLoginTip = true;
      const t = this;
      this.nzModal.warning({
        nzTitle: this.translate.instant('login.text.time_out_title'),
        nzContent: this.translate.instant('login.text.time_out_message'),
        nzClosable: false,
        nzMaskClosable: false,
        nzOkText: this.translate.instant('login.text.time_out'),
        nzOnOk() {
          t.showLoginTip = false;
          t.systemUserService.clearLoginMsg();
          t.router.navigate([t.tokenService.login_url]);
        }
      });
    }
  }

  private getError(returnCode: string): void {
    let str = '服务器内部错误';
    let tips = true;
    switch (returnCode) {
      case '000001':
        str = '服务器内部错误';
        break;
      case '000002':
        str = '入参参数不一致';
        break;
      case '000003':
        str = '需要token鉴权';
        break;
      case '000004':
        str = 'token过期';
        break;
      case '000005':
        str = '非法token';
        break;
      case '000006':
        str = '无数据';
        tips = false;
        break;
      case '000007':
        str = '数据库操作失败';
        break;
      case '000008':
        str = '入参参数缺失';
        break;
      case '000009':
        str = '无可更新的内容';
        break;
      case '000010':
        str = '接口路径不存在';
        break;
      case '000011':
        str = '当前内容不允许修改';
        break;
      case '000012':
        str = '数据已存在';
        break;
      case '000013':
        str = '网络超时,请重试!';
        break;
      case '010000':
        str = '已登录';
        break;
      case '010001':
        str = '未登录';
        break;
      case '010002':
        str = '未授权';
        break;
      case '010003':
        str = '密码错误';
        break;
      case '010004':
        str = '账号已锁定';
        break;
      case '010005':
        str = '验证码错误或失效';
        break;
      case '010006':
        str = '账号已冻结';
        break;
      case '010007':
        str = '账号已被删除';
        break;
      case '020017':
        str = '当前用户信息获取失败';
        break;
      default:
        break;
    }
    // 添加tips字段判断是否弹出错误提示
    if (tips) {
      this.nzMessage.error(str);
    }
  }
}
